package com.gxg.baidu.baidutest;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;

import butterknife.Bind;
import butterknife.ButterKnife;

public class FuGaiWuActivity extends AppCompatActivity {

    @Bind(R.id.mapview)
    MapView mapview;
    private BaiduMap mBaiduMap;
    InfoWindow.OnInfoWindowClickListener listener = null;
    private InfoWindow mInfoWindow;
    private LatLng point;
    private BitmapDescriptor bitmap;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
//        //注意该方法要再setContentView方法之前实现
//        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_fu_gai_wu);
        ButterKnife.bind(this);

        mBaiduMap = mapview.getMap();
        //定义Maker坐标点
        point = new LatLng(39.915, 116.403964);
        //构建Marker图标
        bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.icon_marka);
        button = new Button(getApplicationContext());
        button.setBackgroundResource(R.drawable.popup);

        listener = new InfoWindow.OnInfoWindowClickListener() {
            public void onInfoWindowClick() {

                mBaiduMap.hideInfoWindow();
            }
        };
        button.setText("这是北京天安门");
        button.setTextColor(Color.BLACK);
        button.setWidth(300);
        mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(button), point, -47, listener);
        mBaiduMap.showInfoWindow(mInfoWindow);



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater=getMenuInflater();
        inflater.inflate(R.menu.mapoverlay,menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.option_normal_1:
                mBaiduMap.clear();
                //构建MarkerOption，用于在地图上添加Marker
                OverlayOptions option = new MarkerOptions()
                        .position(point)//位置坐标
                        .perspective(false)//是否开启近大远小效果
                        .animateType(MarkerOptions.MarkerAnimateType.drop)//动画类型
                        .title("这是北京天安门")//设置标题
                        .visible(true)
                        .icon(bitmap);//设置图标

                //在地图上添加Marker，并显示

                mBaiduMap.addOverlay(option);
                return true;
            case R.id.option_normal_2:
                mBaiduMap.clear();
                //定义文字所显示的坐标点
                LatLng llText = new LatLng(39.86923, 116.397428);

                //构建文字Option对象，用于在地图上添加文字
                OverlayOptions textOption = new TextOptions()
                        .bgColor(getResources().getColor(R.color.black))
                        .fontSize(30)
                        .fontColor(getResources().getColor(R.color.white))
                        .typeface(Typeface.SERIF)
                        .text("百度地图SDK")
                        .rotate(0)
                        .position(llText);

                //在地图上添加该文字对象并显示
                mBaiduMap.addOverlay(textOption);
                return true;
            case R.id.option_normal_3:
                mBaiduMap.clear();
                mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(button), point, -47, listener);
                mBaiduMap.showInfoWindow(mInfoWindow);
                return true;
            case R.id.option_normal_4:
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bitmap.recycle();
    }
}
